<?php 
require_once("includes/header.php");

if(array_key_exists('id', $_GET)){
    if($_GET['id']){
        $serviceid = $_GET['id'];
        $servicequery ="SELECT  * FROM [t_ServiceRequests]
                        INNER JOIN [location]
                        ON [location].[ID]= [t_ServiceRequests].[Location_id]
                        where [SR_ID] = '".$serviceid."'";
        $service_result = $DBOBJ->select($servicequery);
       
      
        $location_query = "SELECT TOP 1
             tbl_serviceman.name
                ,tbl_serviceman.email,
                location.service_id, 
                location.lat, 
                location.lon, 
               
                ( 3959 * ACOS( COS( RADIANS(".$service_result[0]['Lattitude'].") ) * COS( RADIANS( lat ) ) * COS( RADIANS( lon ) - RADIANS(".$service_result[0]['Longitude'].") ) + SIN( RADIANS(".$service_result[0]['Lattitude'].") ) * SIN( RADIANS( lat ) ) ) ) AS distance
                FROM [seveneleven].[dbo].[tbl_servicelocation]  as location  
                INNER JOIN tbl_serviceman  ON location.service_id = tbl_serviceman.service_id
                ORDER BY distance ASC";
        $location_result = $DBOBJ->select($location_query);
        
        $locationData = '';
        foreach ($location_result as $loc_value){
           $locationData .="['".$loc_value['name']."<br>  ".$loc_value['email']."',".$loc_value['lat'].", ".$loc_value['lon']."],";
        }
        $finalLocationData = rtrim($locationData, ",");
     
        
    }
}

        




?>
<script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function(){
    var locations = [
        <?php echo $finalLocationData;?>
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(<?php echo $service_result[0]['Lattitude'];?>, <?php echo $service_result[0]['Longitude'];?>),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    });
  </script>
  
  

 

 
    
<!-- main container -->
    <div class="content">
         <div class="container-fluid">
            <div id="pad-wrapper">
                <div class="map-wrapper map section">
                    <div class="row-fluid head">
                        <div class="span12 map_img_container">
                             <div id="map" style="width: 800px; height: 963px;"></div>
                        </div>
                    </div>
                </div>    
             
                
            </div>
        </div>
    </div>
    <!-- end main container -->

	
<?php 
require_once("includes/footer.php");
?>